package server;
import org.omg.CORBA.*;
import org.omg.CosNaming.*;
//import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.PortableServer.*;
import lagern.*;





class ShutdownActions implements Runnable {
	LagerImpl lager;
	ShutdownActions(LagerImpl lager) {
		this.lager = lager;
	}
	
    public void run() {
    	System.out.println("noch nicht fast Beendet");
    	lager.beendeMonitore();
    	try {
			Thread.sleep(250);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	lager.orb.shutdown(true);
    }
 }
public class Server {
	static public ORB orb; 
	static LagerImpl myLager;
	public static POA rootPoa;
	public static void main (String args[]) {
		try {
			orb = ORB.init(args, null);
	
			rootPoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
			rootPoa.the_POAManager().activate();
			
			myLager = new LagerImpl();
			org.omg.CORBA.Object ref = rootPoa.servant_to_reference(myLager);
			
			Lager href = LagerHelper.narrow(ref);
			
			org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
			
			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			
			String name = "Lager";
			NameComponent path[] = ncRef.to_name(name);
			ncRef.rebind(path, href);
			System.out.println("Lager.Server ready and waiting ....");
			
			myLager.orb = orb;
			myLager.shutdownHook = new Thread(new ShutdownActions(myLager));// Erzeugen des Threads
		    Runtime.getRuntime().addShutdownHook(myLager.shutdownHook);// Registrierung
			
			orb.run();
		}catch (Exception e) {
			System.err.println("ERROR: " + e);
			e.printStackTrace(System.out);	
		}
		
		System.out.println("Server Exiting ...");
	}
	
}
